import axios from "./axios";


class Code {

  private codes:any = {};
  private loading:any = {};
  private promises:any = {};

  public load(typ:'code'|'enum',code:string):Promise<any>{
    const key = typ + code;
    if(this.promises[key]){
      return this.promises[key];
    }
    const promise =  new Promise((resolve,reject) =>{
      if(this.codes[key]){
        return resolve(this.codes[key]);
      }
      if(this.loading[key]){
        return;
      }
      this.loading[key] = true;
      axios.get('/api/v1/sys/dict',{params:{type:typ,code}}).then(({data}) => {
        this.loading[key] = false;
        this.codes[key] = data;
        // @ts-ignore
        resolve(data);
      }).catch(e => {
        this.loading[key] = false;
        reject(e);
      });
    });
    return this.promises[key] = promise;
  }


}

export default Code;
